﻿using AntMovement.Core.IParameter;
using AntMovement.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntMovement.Core.Entity;

namespace AntMovement.Parameter
{
    public abstract class TaskBase : ITask
    {
        protected bool _alarmStation = false;
        protected int _step = 0;
        protected IDialogHost _dialogHost;
        protected AntMovement.Parameter.TemporaryParameter _temporary;
        protected IAxisParameter _axisParameter;
        protected ILog _log;
        protected IIOParameter _iop;
        public TaskBase(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
        {
            _iop = iop;
            _dialogHost = dialogHost;
            _temporary = (Parameter.TemporaryParameter)temporary;
            _axisParameter = axisp;
            _log = log;
        }

        /// <summary>
        /// 实时扫描
        /// </summary>
        public abstract void Handle();

        /// <summary>
        /// 复位
        /// </summary>
        public abstract void Resetting();

        /// <summary>
        /// 安全停止
        /// </summary>
        public abstract void SafeStop();

        /// <summary>
        /// 急停
        /// </summary>
        public abstract void EmergencyStop();

        protected void NextStep(int step = 10) => _step += step;
        protected void BackStep(int step = 10) => _step -= 10;

        protected bool SignalOn(int step) => _step == step && _temporary.SystemStatus.Auto;

        #region 行程与脉冲转换
        protected float PulseToDistance(Axis axis) => (float)Math.Round(GetPE(axis) * (double)axis.Status.CurrentPostion, 6, MidpointRounding.AwayFromZero);
        protected float PulseToDistance(long pulse, Axis axis) => (float)Math.Round(GetPE(axis) * (double)pulse, 6, MidpointRounding.AwayFromZero);

        protected long DistanceToPulse(double distance, Axis axis) => (long)Math.Round(distance / GetPE(axis), 6, MidpointRounding.AwayFromZero);

        protected double GetPE(Axis axis)
        {
            double pe = 0;
            if (axis.UnitConversion.EnableGearbox)
            {
                if (axis.UnitConversion.EnableRotationMode)
                {
                    //旋转模式

                }
                else
                {
                    //线性模式
                    pe = (axis.UnitConversion.WeeklyItinerary * axis.UnitConversion.MotorGearRatio) / (axis.UnitConversion.CyclePulse * axis.UnitConversion.WorkingGearRatio);
                }
            }
            else
            {
                pe = axis.UnitConversion.WeeklyItinerary / axis.UnitConversion.CyclePulse;
            }
            return pe;
        }
        #endregion
    }
}
